package fr.afcepf.iziticket.beans;

import java.util.ArrayList;
import java.util.List;

import javax.faces.application.NavigationHandler;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import fr.afcepf.iziticket.entities.Client;
import fr.afcepf.iziticket.services.SigninService;

@ManagedBean(name="mbSignin")
@SessionScoped
public class SigninManagedBean {

	private SigninService service = SigninService.getService();
	
	@ManagedProperty(value="#{mbLogin}")
	private LoginManagedBean mbLogin;
	
	private String login;
	private String nom;
	private String prenom;
	private String pseudonyme;
	private String mail;
	private String mail2;
	private String mdp;
	private String cMdp;
	private List<String> erreurs=new ArrayList<String>();
	
	
	public LoginManagedBean getMbLogin() {
		return mbLogin;
	}
	public void setMbLogin(LoginManagedBean mbLogin) {
		this.mbLogin = mbLogin;
	}
	public String getLogin() {
		return login;
	}
	public void setLogin(String login) {
		this.login = login;
	}
	public List<String> getErreurs() {
		return erreurs;
	}
	public void setErreurs(List<String> erreurs) {
		this.erreurs = erreurs;
	}
	public String getNom() {
		return nom;
	}
	public void setNom(String nom) {
		this.nom = nom;
	}
	public String getPrenom() {
		return prenom;
	}
	public void setPrenom(String prenom) {
		this.prenom = prenom;
	}
	public String getPseudonyme() {
		return pseudonyme;
	}
	public void setPseudonyme(String pseudonyme) {
		this.pseudonyme = pseudonyme;
	}
	public String getMail() {
		return mail;
	}
	public void setMail(String mail) {
		this.mail = mail;
	}
	public String getMail2() {
		return mail2;
	}
	public void setMail2(String mail2) {
		this.mail2 = mail2;
	}
	public String getMdp() {
		return mdp;
	}
	public void setMdp(String mdp) {
		this.mdp = mdp;
	}
	public String getcMdp() {
		return cMdp;
	}
	public void setcMdp(String cMdp) {
		this.cMdp = cMdp;
	}
	
	public String signin(){
		
		erreurs.clear();
		boolean ok=true;
		
		if(!mail.equals(mail2)){
			erreurs.add("Erreur de confirmation de l'email");
			ok=false;
		}
		if(!mdp.equals(cMdp)){
			erreurs.add("Erreur de confirmation du mot de passe");
			ok=false;
		}
		if(!(login!=""&&nom!=""&&prenom!=""&&mail!=""&&mail2!=""&&mdp!=""&&cMdp!="")){
			erreurs.add("Remplissez tous les champs");
			ok=false;
		}
		
		if(ok){
			
			Client client = service.enregistrement(login, mail, cMdp, nom, prenom, pseudonyme);
			
			mbLogin.setUtilisateur(client);
			mbLogin.setConnecte(true);
			
			FacesContext facesContext = FacesContext.getCurrentInstance();
			NavigationHandler myNav = facesContext.getApplication().getNavigationHandler();
		    myNav.handleNavigation(facesContext, null, "/home.xhtml");
			
		}
		
		return "";
	}
}
